/**
 * 
 */
package com.wgo.precise.client.ui.view.util.dialogs;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.wgo.precise.exceptions.InvalidPreciseXmlException;

/**
 * @author Petter L. H. Eide
 * @version $Id: PreciseErrorDialog.java 542 2008-04-28 16:29:43Z petter.eide $
 */
public class PreciseErrorDialog extends ErrorDialog {

	private IStatus status = null ;
	private Text text = null ;
	private boolean textCreated = false ;
    private Clipboard clipboard;
    private int minYSize = 0;
    private int maxYSize = 600;

	public PreciseErrorDialog(Shell parentShell, String dialogTitle, String message, IStatus status) {
		super(parentShell, dialogTitle, message, status, status.getSeverity());
		this.status = status ;
	}

    @Override
	protected void buttonPressed(int id) {
        if (id == IDialogConstants.DETAILS_ID && shouldShowExtendedDetails() ) {
            toggleExtendedDetailsArea();
        } else {
        	super.buttonPressed(id);
        }
    }
	
    
    
	private void toggleExtendedDetailsArea() {
		Point windowSize = getShell().getSize();
		Point oldSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
		if ( 0 == minYSize){
			minYSize = oldSize.y;
			if (minYSize > maxYSize) {
				maxYSize = minYSize + 100;
			}
		}
		if (textCreated) {
			text.dispose();
			textCreated = false;
			getButton(IDialogConstants.DETAILS_ID).setText(IDialogConstants.SHOW_DETAILS_LABEL);
		} else {
			text = createDropDownText((Composite) getContents());
			getButton(IDialogConstants.DETAILS_ID).setText(IDialogConstants.HIDE_DETAILS_LABEL);
		}
		Point newSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
		int finalNewYSize = windowSize.y + (newSize.y - oldSize.y);
		if (minYSize > finalNewYSize) {
			finalNewYSize = minYSize;
		} else if ( maxYSize < finalNewYSize ) {
			finalNewYSize = maxYSize;
		}
		Point finalNewSize = new Point(windowSize.x, finalNewYSize) ;
		getShell().setSize(finalNewSize);
	}

	private Text createDropDownText(Composite parent) {
        // create the list
        text = new Text(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
        // fill the list
        text.setEditable(false);
        text.setText(((InvalidPreciseXmlException)status.getException()).getDebugText());
        GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL 
        		| GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_VERTICAL);
        data.heightHint = text.getLineCount() * text.getLineHeight();
        data.horizontalSpan = 2;
        text.setLayoutData(data);
        text.setFont(parent.getFont());
        Menu copyMenu = new Menu(text);
        MenuItem copyItem = new MenuItem(copyMenu, SWT.NONE);
        copyItem.addSelectionListener(new SelectionListener() {
            /*
             * @see SelectionListener.widgetSelected (SelectionEvent)
             */
            public void widgetSelected(SelectionEvent e) {
                copyToClipboard();
            }

            /*
             * @see SelectionListener.widgetDefaultSelected(SelectionEvent)
             */
            public void widgetDefaultSelected(SelectionEvent e) {
                copyToClipboard();
            }
        });
        copyItem.setText(JFaceResources.getString("copy")); //$NON-NLS-1$
        text.setMenu(copyMenu);
        textCreated = true;
        return text;
	}

	protected boolean shouldShowExtendedDetails() {
		return status.getException() instanceof InvalidPreciseXmlException && null != ((InvalidPreciseXmlException)status.getException()).getDebugText();
	}
	
    /**
     * Copy the contents of the statuses to the clipboard.
     */
    private void copyToClipboard() {
        if (clipboard != null) {
			clipboard.dispose();
		}
        clipboard = new Clipboard(text.getDisplay());
        clipboard.setContents(new Object[] { text.getSelectionText() },
                new Transfer[] { TextTransfer.getInstance() });
    }

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.ErrorDialog#close()
	 */
	@Override
	public boolean close() {
        if (clipboard != null) {
			clipboard.dispose();
		}
		return super.close();
	}
    
    
}
